<template>
  <el-dialog 
    custom-class="selfDialog" 
    :visible.sync="moduleVisible" 
    :close-on-click-modal="false"
    title="编辑"
    width="600px"
    @closed="dialogClosed">
    <div class="module-header">
      <el-row :gutter='20'>
        <el-col :span='4' class="header-label">{{ '标准名称' }}</el-col>
        <el-col :span='14' class="header-label">{{ '客户定义名称' }}</el-col>
        <el-col :span='6' class="header-label">{{ '是否显示' }}</el-col>
      </el-row>
      <el-form ref="moduleForm" :model="list">
        <el-row
          :gutter='20'
          v-for="(column, index) of list.arrList" 
          :key="index">
          <el-col 
            :span="4" 
            :title="column.templateName"
            class="template-name text-overflow">
            {{ column.templateName }}
          </el-col>
          <el-col :span="14">
            <el-form-item 
              :prop="'arrList.' + index + '.customName'" 
              :rules='getRules()'>
              <el-input
                v-model="column.customName"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span='6'>
            <el-switch v-model="column.show"></el-switch>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" @click="moduleVisible = false">取消</el-button>
      <el-button size="mini" type="primary" @click="moduleModify">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { 
  validateChEng
} from '@/utils/validate.js'

export default {
  props: {
    columnList: Object,
  },
  data() {
    return {
      moduleVisible: false,
      list: {
        arrList: []
      },
    }
  },
  created() {
    
  },
  methods: {
    show() {
      // 防止告警 :model 绑定Object
      this.list.arrList = []
      this.list.arrList = this.columnList.columnList.concat(this.columnList.propertyList)
      this.moduleVisible = true
    },
    close() {
      // resetFields 恢复上一次的值 置空解决
      this.$refs.moduleForm && this.$refs.moduleForm.resetFields()
      this.list.arrList = []
      this.moduleVisible = false
    },
    dialogClosed() {
      this.$emit('dialogClose')
      this.close()
    },
    moduleModify() {
      this.$refs.moduleForm && this.$refs.moduleForm.validate(valid => {
        if (valid) {
          this.$emit('moduleModify')
        } else {
          return false
        }
      })
    },
    getRules() {
      return [
        { min: 1, max: 64, message: '长度在64个字符以内', trigger: 'blur' },
        { validator: validateChEng, trigger: 'blur' }
      ]
    },
  },
}
</script>

<style lang="scss" scoped>
.module-header {
  padding: 20px;
  max-height: 400px;
  overflow: auto;
  .header-label {
    font-size: 0.9rem;
    font-weight: bold;
  }
  .template-name {
    padding-top: 10px;
  }
  .el-row {
    margin-bottom: 10px;
    .el-switch {
      padding-top: 14px;
    }
  }
}
</style>
